package com.example.shiwu.service;

import android.app.Service;

import com.example.shiwu.db.dao.ArrangeDao;
import com.example.shiwu.db.dao.DeviceDao;
import com.example.shiwu.db.dao.ItemsDao;
import com.example.shiwu.db.dao.ReceiveVolEleDao;
import com.example.shiwu.db.dao.SendsDao;
import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.model.Arrange;
import com.example.shiwu.db.model.Device;
import com.example.shiwu.db.model.Items;
import com.example.shiwu.db.model.ReceiveVolEle;
import com.example.shiwu.db.model.Sends;
import com.example.shiwu.manager.DatabaseHelper;
import com.example.shiwu.util.FileDownUtil;

import java.util.ArrayList;
import java.util.List;

public class ItemsService {

    private DatabaseHelper dbHelper;
    private ItemsDao<Items> itemsDao;
    private ArrangeDao<Arrange> ArrangeDao;
    private DeviceDao<Device> deviceDao;
    private ReceiveVolEleDao<ReceiveVolEle> receiveVolEleDao;
    private SendsDao<Sends> SendsDao;
    private ArrayList<Items> listItems = new ArrayList<>();

    private static final int PAGE_SIZE = 10;
    private static final int TOTAL_PAGE = 0;



    /**获取单个数据
     * @param items
     * @return
     */
    public List<Items> getItems(Items items) {
        listItems = new ArrayList<>();
        itemsDao = BaseDaoFactory.getInstance().createBaseDao(ItemsDao.class, Items.class);
        listItems.addAll(itemsDao.query(items));
        return listItems;
    }

    /**分页获取数据
     * @param items
     * @return
     */
    public List<Items> getItemsList(Items items,Integer startIndex,Integer limit) {
        itemsDao = BaseDaoFactory.getInstance().createBaseDao(ItemsDao.class, Items.class);
        listItems = new ArrayList<>();
        listItems.addAll(itemsDao.query(items,null,"create_time desc",null,startIndex,limit));
        return listItems;
    }

    public boolean save(Items items) {
        itemsDao = BaseDaoFactory.getInstance().createBaseDao(ItemsDao.class, Items.class);
        long size = itemsDao.insert(items);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public boolean updade(Items items,Items where) {
        itemsDao = BaseDaoFactory.getInstance().createBaseDao(ItemsDao.class, Items.class);

        long size = itemsDao.update(items,where);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public Integer delete(Items items) {
        itemsDao = BaseDaoFactory.getInstance().createBaseDao(ItemsDao.class, Items.class);
        int size = itemsDao.delete(items);
        return size;
    }

    /**
     * 删除项目同时，删除子表及cvs文件
     * @param items
     * @return
     */
    public Integer deleteAll(Items items) {
        int item_id = items.getId();
        itemsDao = BaseDaoFactory.getInstance().createBaseDao(ItemsDao.class, Items.class);
        ArrangeDao = BaseDaoFactory.getInstance().createBaseDao(ArrangeDao.class, Arrange.class);
        deviceDao = BaseDaoFactory.getInstance().createBaseDao(DeviceDao.class, Device.class);
        receiveVolEleDao = BaseDaoFactory.getInstance().createBaseDao(ReceiveVolEleDao.class, ReceiveVolEle.class);
        SendsDao = BaseDaoFactory.getInstance().createBaseDao(SendsDao.class, Sends.class);
        int arrangeSize = ArrangeDao.delete(new Arrange(item_id));
        int deviceSize = deviceDao.delete(new Device(String.valueOf(item_id)));
        int receiveVolEleSize = receiveVolEleDao.delete(new ReceiveVolEle(item_id));
        int sendsSize = SendsDao.delete(new Sends(item_id));
        int size = itemsDao.delete(items);
        return size;
    }
}
